convert_to_number Subroutine

public subroutine convert_to_number(input_str, number, ios)

Converts user input (cards or numbers) into numeric values. Handles card values such as 'A', 'J', 'Q', 'K'.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input_str

Input: String representing the number or card value.

real, intent(out) :: number

Output: The corresponding numeric value after conversion.

integer, intent(out) :: ios

Output: I/O status indicator (0 for success).


Called by

proc~~convert_to_number~~CalledByGraph proc~convert_to_number convert_to_number program~game24_ultra game24_ultra program~game24_ultra->proc~convert_to_number

Source Code

    subroutine convert_to_number(input_str, number, ios)
        !! Converts user input (cards or numbers) into numeric values.
        !! Handles card values such as 'A', 'J', 'Q', 'K'.
        character(len=*), intent(in) :: input_str
        !! Input: String representing the number or card value.
        real, intent(out)            :: number
        !! Output: The corresponding numeric value after conversion.
        integer, intent(out)         :: ios
        !! Output: I/O status indicator (0 for success).
        character(len=1)             :: first_char
        !! Temporary variable to hold the first character of the input.
        real                         :: temp_number
        !! Temporary variable to store the numeric value.

        ios = 0  ! Reset the I/O status to 0 (valid input by default)
        first_char = input_str(1:1)

        select case (first_char)
        case ('A', 'a')
            number = 1.0
        case ('J', 'j')
            number = 11.0
        case ('Q', 'q')
            number = 12.0
        case ('K', 'k')
            number = 13.0
        case default
            read (input_str, *, iostat=ios) temp_number  ! Attempt to read a real number

            ! If input is not a valid real number or is not an integer, set ios to 1
            if (ios /= 0 .or. mod(temp_number, 1.0) /= 0.0) then
                ios = 1  ! Invalid input
            else
                number = temp_number  ! Valid integer input
            end if
        end select
    end subroutine convert_to_number